var util = require('../../libs/util.js');
var app = getApp();
var ringChart = null;
Page({
  data: {
    userInfo: {},
    truename: "",
    hasData: false,
    cardData: {
      balance: 0,
      mile: 0,
      card: 0,
      plate:'',
      prevMonthCost: 0,
      thisMonthCost: 0
    }
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onPullDownRefresh: function () {
    //wx.stopPullDownRefresh()
  },
  onLoad: function (e) {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName +"--加油管理" });

    that.setData({
      userInfo: app.globalData.userInfo,
      truename: app.globalData.truename
    });
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          middle_height: res.windowHeight - 100,
          client_width: res.windowWidth,
          client_height: res.windowHeight
        });
      }
    });
    that.loadData();
  },
  loadData: function () {
    var that = this;
    wx.showLoading({
      title: '请稍候...',
    });
    util.getData("vehicle_gasrecords", { openid: app.globalData.openid }, function (res) {
      console.log(res.data)
      if (res.data.success) {
        var data = res.data.data;
        that.setData({
          cardData: {
            balance: res.data.cardinfo.balance,
            mile: res.data.cardinfo.mile,
            cardId: res.data.cardinfo.ID,
            card: res.data.cardinfo.card,
            plate: res.data.cardinfo.plate,
            prevMonthCost: (data[2][0].cost || 0).toFixed(0),
            thisMonthCost: (data[1][0].cost || 0).toFixed(0)
          },
          gasrecords: data[0]
        });
      }
      wx.hideLoading();
    });
  },
  addRecord: function () {
    var that = this;
    wx.redirectTo({
      url: '/pages/driver/gasrecords_add?card=' + that.data.cardData.cardId
    })
    // wx.showActionSheet({
    //   itemList: ['本人油卡加油', '他人油卡加油'],
    //   success: function (e) {
    //     if (e.tapIndex == 0) {
    //       wx.redirectTo({
    //         url: '/pages/driver/gasrecords_add?card=' + that.data.cardData.cardId
    //       })
    //     } else if (e.tapIndex == 1) {
    //       wx.redirectTo({
    //         url: '/pages/driver/gasrecords_add'
    //       })
    //     }
    //   }
    // })
  },
  gotoDriverHome: function () {
    wx.redirectTo({
      url: '/pages/driver/orderList'
    })
  },
  gotoDriverInfo: function () {
    wx.redirectTo({
      url: '/pages/driver/driverInfo'
    })
  },
  gotoCurrentOrder: function () {
    wx.redirectTo({
      url: '/pages/driver/currentOrder'
    })
  },
  gotoHistoryOrder: function () {
    wx.redirectTo({
      url: '/pages/driver/historyOrder'
    })
  }
})
